from rest_framework import serializers
from .models import ShopsModel,GoodTypeModel,GoodsModel


# 商家序列化器
class ShopsSer(serializers.ModelSerializer):        # 只用于序列化输入 # 去掉两边的空格
    password = serializers.CharField(max_length=256, write_only=True, trim_whitespace=True)

    class Meta:
        model = ShopsModel
        fields = ["id","nickname","avatar_url","intro","address","mobile","password","work_time","start_price","date_joined","last_login"]
        read_only_fields = ["id","date_joined"]


# 菜品类别
class GoodTypeSer(serializers.ModelSerializer):
    shops = serializers.PrimaryKeyRelatedField(queryset=ShopsModel.objects.all())
    shops_name = serializers.SerializerMethodField()

    def get_shops_name(self, obj):
        return obj.shops.nickname

    class Meta:
        model = GoodTypeModel
        fields = "__all__"


# 菜品
class GoodSer(serializers.ModelSerializer):
    type = serializers.PrimaryKeyRelatedField(queryset=GoodTypeModel.objects.all())
    type_name = serializers.SerializerMethodField()

    def get_type_name(self, obj):
        return obj.type.name

    class Meta:
        model = GoodsModel
        fields = "__all__"

